<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class CreateBaoliaoShardedTables extends Command
{
    protected $signature = 'baoliao:create-sharded-tables';
    protected $description = '创建爆料帖子的分表';
    
    public function handle()
    {
        $baseTable = 'module_baoliao_post';
        
        // 创建时间分表（最近12个月）
        $current = time();
        for ($i = 0; $i < 12; $i++) {
            $time = strtotime("-$i months", $current);
            $suffix = date('Ym', $time);
            $tableName = "{$baseTable}_{$suffix}";
            if (!Schema::hasTable($tableName)) {
                $sql="CREATE TABLE {$tableName} LIKE {$baseTable}";
                //die($sql);
                DB::statement($sql);
                $this->info("创建时间分表: {$tableName}");
            }
        }
        //die("666666");
        // 创建热度分表
        for ($i = 1; $i <= 10; $i++) {
            $tableName = "{$baseTable}_hot_{$i}";
            
            if (!Schema::hasTable($tableName)) {
                $sql="CREATE TABLE {$tableName} LIKE {$baseTable}";
                DB::statement($sql);
                $this->info("创建热度分表: {$tableName}");
            }
        }
        
        $this->info('所有分表创建完成');
    }
}
